﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ZQNB.BaseLib.Core.Models;
using ZQNB.Common;

namespace ZQNB.BaseLib.Core
{
    public class WidgetMetaHelper
    {
        private readonly WidgetDetector _widgetDetector;
        private readonly CoreDataHelper _fileDbHelper;

        public WidgetMetaHelper(WidgetDetector widgetDetector, CoreDataHelper fileDbHelper)
        {
            _widgetDetector = widgetDetector;
            _fileDbHelper = fileDbHelper;
        }

        public List<WidgetMeta> ReadWidgetMetas()
        {
            var widgets = new List<WidgetMeta>();

            var basePath = MyPathHelper.MapIISServerPath("~/");
            List<string> files = MyIOHelper.GetFiles(basePath, "_*.cshtml", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                if (_widgetDetector.IsWidget(file))
                {
                    var widgetMeta = new WidgetMeta();
                    widgetMeta.PartialViewName = file.Replace(basePath, "~/").Replace("\\", "/");
                    widgetMeta.Name = _widgetDetector.GetWidgetInfo(file);
                    widgets.Add(widgetMeta);
                }
            }

            return widgets;
        }

        public void SyncWidgetMetas(List<WidgetMeta> metas)
        {
            _fileDbHelper.Save(metas);
        }
    }

    public class WidgetDetector
    {
        /// <summary>
        /// 判断是否是部件
        /// @* Widget : xxx *@
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool IsWidget(string filePath)
        {
            var lines = File.ReadLines(filePath).ToList();
            for (int i = 0; i < lines.Count; i++)
            {
                var text = lines[i].Trim();
                if (text == "")
                {
                    continue;
                }
                if (text.StartsWith("@*") && text.EndsWith("*@"))
                {
                    if (text.NbContains("widget"))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 获取Widget信息
        /// @* Widget : xxx *@
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string GetWidgetInfo(string filePath)
        {
            string name = "";
            var lines = File.ReadLines(filePath).ToList();
            for (int i = 0; i < lines.Count; i++)
            {
                var text = lines[i].Trim();
                if (text == "")
                {
                    continue;
                }
                if (text.StartsWith("@*") && text.EndsWith("*@"))
                {
                    if (text.NbContains("widget"))
                    {
                        var temp = text.Replace("@*", "").Replace("*@", "").Trim(); ;
                        // Widget : xxx
                        name = temp.Split(':')[1].Trim();
                    }
                }
                break;
            }
            return name;
        }
    }
}
